using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SpaceShooter.Fighters;
using SpaceShooter.SceneCamera;
using SpaceShooter.Settings;
using SpaceShooter.Background;

namespace SpaceShooter
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        ScrollingBackground scrollBackground;
        Fighter fighter;
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            GameSettings.CameraSettings.read();
            GameSettings.SystemSettings.read();
            graphics.PreferredBackBufferHeight = GameSettings.SystemSettings.BufferHeight;
            graphics.PreferredBackBufferWidth = GameSettings.SystemSettings.BufferWidth;

            graphics.ApplyChanges();
            
            base.Initialize();
        }

        //we should remove the path hard code
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            fighter = new Fighter();
            fighter.Load("Fighter//Fighter",Content);

            Camera.LoadParameters();

            scrollBackground = new ScrollingBackground();
            scrollBackground.Load(GraphicsDevice, "Background//starfield", Content);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            fighter.Update();
            scrollBackground.Update(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            ///2D Drawing Phase
            spriteBatch.Begin();
            scrollBackground.Draw(spriteBatch);
            spriteBatch.End();

            GameSettings.ResetRenderStates(GraphicsDevice);

            ///3D Drawing Phase
            fighter.Draw(GraphicsDevice, Camera.ViewMatrix, Camera.ProjectionMatrix);

            base.Draw(gameTime);
        }
    }
}
